<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\PositionSupervise;
use App\Model\PositionSuperviseSheet;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;

/**
 * @Command
 */
class UpdateSuperviseScore extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('update-score:supervise');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('更新监督的分值');
    }

    public function handle()
    {
        $this->line('start update', 'info');
        $lists = PositionSupervise::query()->orderBy('id','asc')->get();
        foreach($lists as $item){
            $full_score = PositionSuperviseSheet::query()->join('position_paper',function($join){
                $join->on('position_paper.id','=','position_supervise_sheet.paper_id');
            })->where('supervise_id',$item->id)->sum('position_paper.score');
            $item->full_score = $full_score;
            $item->save();
            $this->line('监督=>'.$item->id.'=>更新完毕');
        }
        $this->line('end update', 'info');
    }
}
